package com.renwei.yunlong.utils;

import android.text.TextUtils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Project :  yunaandroid.
 * Package name: com.renwei.yunlong.utils
 * Created by :  Benjamin.
 * Created time: 2017/10/26 14:41
 * Changed by :  Benjamin.
 * Changed time: 2017/10/26 14:41
 * Class description:
 */

public class DateTimeUtils {

    //date 2 string
    public static String date2String(Date date, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    //string 2 date
    public static Date string2Date(String str, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {
            return format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    //string 2 long
    public static Long string2Long(String str, String pattern) {
        ///String转化为Timestamp:
        SimpleDateFormat df1 = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = df1.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Timestamp ts = new Timestamp(date.getTime());
        return ts.getTime() / 1000;
    }

    // long 2 string
    public static String long2String(long timeStamp, String reg) {
        SimpleDateFormat sdf = new SimpleDateFormat(reg);
        String date = sdf.format(timeStamp * 1000);
        return date;
    }

    /**
     * 工单历史时间比较  存在两种格式
     */
    public static Long compareTime(String str) {
        if (TextUtils.isEmpty(str)) {
            return 0L;
        }
        if (str.length() == 10) {
            return string2Long(str, "yyyy-MM-dd");
        } else if (str.length() == 16) {
            return string2Long(str, "yyyy-MM-dd HH:mm");
        } else {
            return string2Long(str, "yyyy-MM-dd HH:mm:ss");
        }
    }

    /**
     * 消息列表  返回格式 HH:ss
     */
    public static String getHHss(String str) {
        return date2String(string2Date(str, "yyyy-MM-dd HH:mm:ss"), "HH:mm");
    }

    /**
     * 工单历史  返回格式 MM/dd HH:mm
     */
    public static String getMMddHHmm(String str) {
        return date2String(string2Date(str, "yyyy-MM-dd HH:mm:ss"), "MM/dd HH:mm");
    }

    /*
    * 将时间戳转换成当前时间
    * 如果时间为今日时间 则返回 24小时计算
    * 如果为昨天就显示昨天的 24小时
    * 其他的只显示 年月日不显示时分秒
    */
    public static String stampToDateNow(Long time) {
        if (time==0){
            return "";
        }
        //第一步判断时间是否是今天。
        if (isToday(time)) {
            String result = long2String(time, "HH:mm");
            return result;
        } else if (isYesterday(time)) {
            String result = long2String(time, "昨天 HH:mm");
            return result;
        } else {
            String result = long2String(time, "MM-dd HH:mm");
            return result;
        }
    }

    //判断时间是否是今天
    public static boolean isToday(long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = format.parse(long2String(time, "yyyy-MM-dd"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        Date today = calendar.getTime();
        return format.format(today).equals(
                format.format(date));
    }

    //判断时间是否是昨天
    public static boolean isYesterday(Long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = format.parse(long2String(time, "yyyy-MM-dd"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 1);
        Date today = calendar.getTime();
        return format.format(today).equals(
                format.format(date));
    }
}
